﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using Wjtour.BLL;

namespace Wjtour.Web.Front
{
    /// <summary>
    /// Summary description for login
    /// </summary>
    public class memberlogin : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            var account = context.Request["account"];
            var code = context.Request["code"];
            var pwd = context.Request["pwd"];

            Result r = new Result();
            if (string.IsNullOrWhiteSpace(account))
            {
                r.Errcode = "1";
                r.Errmsg = "账号不能为空！";
            }
            else if (string.IsNullOrWhiteSpace(pwd))
            {
                r.Errcode = "2";
                r.Errmsg = "密码不能为空！";
            }
            else if (string.IsNullOrWhiteSpace(code))
            {
                r.Errcode = "2";
                r.Errmsg = "验证码不能为空！";
            }
            else if (code.ToUpper() != context.Session["CheckCode"].ToString())
            {
                r.Errcode = "2";
                r.Errmsg = "验证码输入不正确！";
            }
            else
            {
                Wjtour.BLL.member_bll m_bll = new member_bll();
                Wjtour.Domain.member member = m_bll.CheckEmail(account.Trim());
                if (member == null)
                {
                    r.Errcode = "11";
                    r.Errmsg = "该邮箱没有注册过！";
                }
                else
                {
                    member = m_bll.Login(account.Trim(), pwd.Trim());
                    if (member == null)
                    {
                        r.Errcode = "21";
                        r.Errmsg = "密码出错！";
                    }
                    else
                    {
                        r.Errcode = "0";
                        r.Errmsg = "登录成功！";
                        context.Session["CurrentMember"] = member;
                    }
                }
            }

            JavaScriptSerializer serialzier = new JavaScriptSerializer();
            string output = serialzier.Serialize(r);
            context.Response.Write(output);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    [Serializable]
    public class Result
    {
        public string Errcode { get; set; }
        public string Errmsg { get; set; }
    }
}